package com.ruoyi.system.task;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.ruoyi.common.utils.QiniuCloudUtil;
import com.ruoyi.system.domain.Balance;
import com.ruoyi.system.mapper.HomeMapper;
import com.ruoyi.system.mapper.WatehouseMaterialMapper;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * @ClassName: MyTask
 * @Description: 定时任务
 * @Author: liu
 * @Date: 2021/5/13 14:50
 */
@Configuration
@EnableScheduling
public class MyTask {

    @Autowired
    private HomeMapper homeMapper;
    @Autowired
    private WatehouseMaterialMapper watehouseMaterialMapper;

    @Scheduled(cron = "0 0 0 1 * ?")
    public void sendData() {
        //获取当前时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
        //当前月份
        String time = df.format(new Date());
        try {
            //月份减一
            Date date = df.parse(time);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.MONTH, -2);
            //上上个月 查结余
            String format = df.format(calendar.getTime());
            //上月结余
            Double currentBalance=homeMapper.selectCurrentBalance(format);
            //上个月 查用量
            calendar.add(Calendar.MONTH, +1);
            String format1 = df.format(calendar.getTime());
            //甲供出库
            Double supplyOut=homeMapper.selectSupplyOut(format1);
            //乙购出库
            Double purchaseOut=homeMapper.selectPurchaseOut(format1);
            //甲供入库
            Double supplyDelivery=homeMapper.selectSupplyDelivery(format1);
            //乙购入库
            Double purchaseDelivery=homeMapper.selectPurchaseDelivery(format1);
            //判空
            if (null==currentBalance){
                currentBalance=0D;
            }
            if (null==supplyOut){
                supplyOut=0D;
            }
            if (null==purchaseOut){
                purchaseOut=0D;
            }
            if (null==supplyDelivery){
                supplyDelivery=0D;
            }
            if (null==purchaseDelivery){
                purchaseDelivery=0D;
            }
            Balance balance = new Balance();
            Double sumMoney = watehouseMaterialMapper.querySumMoney();
            DecimalFormat decimalFormat = new DecimalFormat("######0.00");
            balance.setBalance(Double.valueOf(decimalFormat.format(new BigDecimal(sumMoney.toString()))));
            balance.setCreatetime(format1 + "-01");
            homeMapper.insertBalance(balance);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    @Scheduled(cron = "0 0 0 1 * ?")
    public void excel() {
        System.out.println("进来了");
        FileOutputStream out = null;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
        DecimalFormat decimalFormat = new DecimalFormat("######0.00");
        //当前月份
        String time = df.format(new Date());
        try {
            //月份减一
            Date date = df.parse(time);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.MONTH, -1);
            //上个月
            String format = df.format(calendar.getTime());
            List<Map> maps = watehouseMaterialMapper.endexpectMaterial();
            Double sumMoney = watehouseMaterialMapper.querySumMoney();
            for (int i = 0; i < maps.size(); i++) {
                maps.get(i).put("id", i + 1);
            }
            // 表格使用的数据
            Map map = new HashMap();
            map.put("data", maps);//放入表一数据
            map.put("date", format);//放入表一数据
            map.put("sum",decimalFormat.format(new BigDecimal(sumMoney.toString())));//放入表一数据
            format += ".xlsx";
            TemplateExportParams params = new TemplateExportParams("/home/ruoyi/excel/期末材料.xlsx");
            Workbook workbook = ExcelExportUtil.exportExcel(params, map);
            out = new FileOutputStream("/home/ruoyi/excel/" + format);
            workbook.write(out);
            File file1 = new File("/home/ruoyi/excel/" + format);
            QiniuCloudUtil.upload(file1, format);
            out.flush();
            file1.delete();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }
    }
}